#include<vector>
#include<algorithm>
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right)
    {
    }
};
class Solution {
public:
    bool isB = true;
    bool isBalanced(TreeNode* root) {
        isB = true;
        HT(root);
        return isB;
    }
    int HT(TreeNode* root)
    {
        if (!isB||root==nullptr)
        {
            return 0;
        }
        
        int lf = HT(root->left);
        int rt = HT(root->right);
        if (std::abs(lf-rt)>=2)
        {
            isB = false;
            return 0;
        }
        else
        {
            return std::max(lf,rt)+1;
        }
    }
};

